package com.zlcx.tz.live.processor.impl;

import com.zlcx.tz.live.client.UserClient;
import com.zlcx.tz.live.exception.CheckTargetException;
import com.zlcx.tz.live.exception.UserTimeLearningException;
import com.zlcx.tz.live.processor.TakeSeatPostProcessor;
import com.zlcx.tz.live.vo.LearnTimeParamVO;
import com.zlcx.tz.live.vo.ResultVO;
import com.zlcx.tz.live.vo.TimeLearnRoomSeatsDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import static com.zlcx.tz.live.constant.ResultStatus.DEL_TARGET_ERROR;
import static com.zlcx.tz.live.vo.LearnTimeParamVO.TypeEnums.TIME_LEARN_ROOMING;

/**
 * @author: anjl
 * @description: （同步）记录学习记录
 * @data: 2020-09-27
 **/
@Slf4j
@Order(10)
@Component
public class StartTimingPostProcessor implements TakeSeatPostProcessor<TimeLearnRoomSeatsDTO> {

    @Override
    public void processor(TimeLearnRoomSeatsDTO seat) {

        Asserts.notNull(seat, "roomUser is null !");
        log.info("计时房间操作-开始-记录学习计时操作 room = {}", seat);
        LearnTimeParamVO learnTime = LearnTimeParamVO.builder()
                .userId(seat.getUserId())
                .roomId(seat.getRoomId())
                .startTime(DateFormatUtils.format(seat.getSeatingTime(), "yyyy-MM-dd HH:mm:ss"))
                .lianMaiId(seat.getId())
                .studyInfoName(seat.getStudyContent())
                .type(TIME_LEARN_ROOMING.code)
                .contentId(seat.getContentId())
                .build();

        ResultVO result = userClient.addUserLearnTime(learnTime);
        if (result.getCode().equals(DEL_TARGET_ERROR.getCode())) {
            throw new CheckTargetException("请先选择目标");
        }
        log.info("计时房间操作-用户开始学习计时结果 seat = {} result = {}", seat, result);

    }

    @Autowired
    private UserClient userClient;

}
